Mapbox的Geocoding API主要用於將地址或地名轉換為經緯度,以及將經緯度反向轉換為地址
Geocoding API是一個基於HTTP的服務,url的格式為
https://api.mapbox.com/geocoding/v5/mapbox.places/{location}.json?access_token=your_mapbox_token
若是將經緯度反向轉換為地址
https://api.mapbox.com/geocoding/v5/mapbox.places/{longitude},{latitude}.json?access_token=your_mapbox_token
這邊有使用fetch傳回資料,主要資料都在features內
而features內的資料為以下
{
"id": "poi.936302887503",
"type": "Feature",
"place_type": [
"poi"
],
"relevance": 1,
"properties": {
"foursquare": "4bfbb8c9f214a5934f7eaf0e",
"landmark": true,
"address": "松高路11號6樓",
"category": "hunan restaurant, hunan food, chinese restaurant, chinese food, restaurant"
},
"text": "1010新湘菜館 Pop Hunan Cuisine",
"place_name": "1010新湘菜館 Pop Hunan Cuisine, 松高路11號6樓, 臺北市, Taipei 110, Taiwan",
"matching_place_name": "1010新湘菜館 Pop Hunan Cuisine, 松高路11號6樓, Taipei, Taipei 110, Taiwan",
"center": [
121.56548,
25.039225
],
"geometry": {
"coordinates": [
121.56548,
25.039225
],
"type": "Point"
},
"context": [
{
"id": "neighborhood.2895079",
"mapbox_id": "dXJuOm1ieHBsYzpMQ3pu",
"text": "興雅"
},
{
"id": "postcode.61159",
"mapbox_id": "dXJuOm1ieHBsYzo3dWM",
"text": "110"
},
{
"id": "locality.63605479",
"mapbox_id": "dXJuOm1ieHBsYzpBOHFLNXc",
"text": "信義區"
},
{
"id": "place.3057895",
"mapbox_id": "dXJuOm1ieHBsYzpMcWpu",
"wikidata": "Q1867",
"text": "臺北市"
},
{
"id": "region.58599",
"mapbox_id": "dXJuOm1ieHBsYzo1T2M",
"wikidata": "Q1867",
"short_code": "TW-TPE",
"text": "Taipei"
},
{
"id": "country.8935",
"mapbox_id": "dXJuOm1ieHBsYzpJdWM",
"wikidata": "Q865",
"short_code": "tw",
"text": "Taiwan"
}
]
}
內容有
id:地點的唯一識別符號。
place_type:地點類型,如 "poi"(地標、餐廳等)。
properties:描述該地點的屬性,如地址、餐廳分類、Foursquare ID 等。
center 和 geometry:該地點的經緯度座標。
context:提供地點的上下文,如鄰近的區域、郵遞區號、城市等。
geometry就是轉換完的經緯度